import {Toast} from 'mint-ui';
function getQueryString() {
  let name, value;
  let str = location.href; //取得整个地址栏
  let num = str.indexOf("?")
  str = str.substr(num + 1); //取得所有参数
  let arr = str.split("&"); //各个参数放到数组里
  for (let i = 0; i < arr.length; i++) {
    num = arr[i].indexOf("=");
    if (num > 0) {
      name = arr[i].substring(0, num).toLowerCase();
      value = arr[i].substr(num + 1);
      this[name] = value;
    }
  }
}

/**
 * 存储localStorage
 */
const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
  if (!name) return;
  const dataStore = window.localStorage.getItem(name);
  return JSON.parse(dataStore);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}

/* 控制台打印 */
const log = name => {
  console.log(name);
}
//公用消息转化
const transNews = (msg) => {
  //console.log('msg',msg)
  let allMsg = '';
  msg.forEach((ele, index) => {
    switch (ele.type) {
      case 'text':
      msg[index] = ele.text;
        break
      case 'face':
        msg[index] = '[表情]';
        break;
      case 'img':
        msg[index] = '[图片]';
        break;
      case 'file':
        msg[index] = '[文件]';
        break;
      case 'audio':
        msg[index] = '[语音]';
        break;
      case 'position':
        msg[index] = '[地理位置]';
        break;
      case 'tip':
        msg[index] = '[群提示消息]';
        break;
      case 'custom':
      msg[index] = '[自定义消息]';
      /*default:
       return ele
       break;*/
    }
    allMsg += msg[index];
  });
  return allMsg;
}

const transTime = (S,AM) => {
  let T = new Date(1E3 * S),
      Format = Q => {return Q < 10 ? '0' + Q : Q},
      result = Format(T.getHours()) + ':' + Format(T.getMinutes());
  let oneDay = 60 * 60 * 24;//s
  let zeroTime = new Date().setHours(0,0,0,0)/1000;
  let passTwo = zeroTime - oneDay;//昨天凌晨
  if(S > passTwo && S < zeroTime){
    result ='昨天 ' + result;
  }else if(S < zeroTime){
    let month = T.getMonth() + 1,
        days = T.getDate();
        result = month + '月' + days + '日 ' + result;
  }else{
    if(typeof AM !== 'undefined'){
      let tHour = T.getHours(),
          tDetail = tHour > 12 ? '下午 ':'上午 ';
      result = tDetail + result;
    }
  }
  return result;
}

const isIOS = () =>{ 
  let u = navigator.userAgent, app = navigator.appVersion;   
  //let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器   
  return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端   
}

const timeStamp = () => {return parseInt(new Date().getTime()/1000)};
const defaultImg = 'this.src="' + require('../img/my@3x.png') + '"';
const defaultGroupImg = 'this.src="' + require('../img/the@3x.png') + '"';
const errInfo = () => {Toast("网络错误,请稍后再试")};

const Client = {
  getQueryString,
  setStore,
  getStore,
  removeStore,
  log,
  transNews,
  transTime,
  defaultImg,
  defaultGroupImg,
  timeStamp,
  errInfo,
  isIOS
}
export default Client